package com.hzz.mybatisplusdemo.config.sqlInjector;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.util.cnfexpression.MultipleExpression;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：hzz
 * @description：TODO
 * @date ：2021/11/8 13:59
 */
public class MyTenantLineInnerInterceptor extends TenantLineInnerInterceptor {

    public MyTenantLineInnerInterceptor(TenantLineHandler tenantLineHandler) {
        super(tenantLineHandler);
    }

    /**
     * 重构mybatispuls的多租户改为支持in
     * @param currentExpression
     * @param table
     * @return
     */
    @Override
    protected Expression builderExpression(Expression currentExpression, Table table) {
        Column aliasColumn = this.getAliasColumn(table);
        boolean presenceOfField=true;

        if(presenceOfField) {
            InExpression inExpression = new InExpression();
            inExpression.setLeftExpression(aliasColumn);
            inExpression.setRightExpression(getTenantLineHandler().getTenantId());
            if (currentExpression == null) {
                return inExpression;
            } else {
                return currentExpression instanceof OrExpression ? new AndExpression(new Parenthesis(currentExpression), inExpression) : new AndExpression(currentExpression, inExpression);
            }
        }else{
              return currentExpression;
        }

    }

    @Override
    protected void processInsert(Insert insert, int index, String sql, Object obj) {

    }

    @Override
    protected void processUpdate(Update update, int index, String sql, Object obj) {

    }

    @Override
    protected void processDelete(Delete delete, int index, String sql, Object obj) {

    }

    @Override
    protected void processInsertSelect(SelectBody selectBody) {

    }

    public static void main(String[] args) {
        InExpression inExpression=new InExpression();
        inExpression.setLeftExpression(new Column("id"));

        List<Expression> childlist =new ArrayList<>();
        childlist.add(new StringValue("1"));
        childlist.add(new StringValue("2"));
        MultipleExpression multipleExpression=new MultipleExpression(childlist) {
            @Override
            public String getStringExpression() {
                return ",";
            }
        };
//        MultiExpressionList multiExpressionList=new MultiExpressionList();
////        List<ExpressionList> expressionLists =new ArrayList<>();
////        expressionLists.add(new ExpressionList(new StringValue("1"),new StringValue("2")));
////        multiExpressionList.setExpressionLists(expressionLists);
////        inExpression.setRightItemsList(multiExpressionList );
        inExpression.setRightExpression(multipleExpression);
        System.out.println(1);
    }
}
